package com.uziot.bucket.service;

import com.uziot.bucket.dao.mapper.PersionHouseMapper;
import com.uziot.galaxy.common.framework.component.AbstractExtractDataListCmp;
import com.uziot.galaxy.common.framework.component.AbstractExtractDataPageCmp;
import com.uziot.galaxy.common.framework.component.ExtractDataCmp;
import com.uziot.galaxy.common.util.date.DateUtils;
import com.uziot.galaxy.common.util.sql.SqlUtil;
import com.uziot.galaxy.dbtools.extract.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author shidt
 * @version V1.0
 * @className PersonHouseDataServiceConfig
 * @date 2022-06-01 23:42:58
 * @description
 */

@Configuration
public class PersonHouseDataServiceConfig {

    @Bean
    public AbstractExtractDataListCmp baseExtractListCmpScott() {
        String filePath = "D:\\";
        String fileName = "SCOTT_EMP_2022060119900429.txt";
        String[] dataTableNames = new String[]{"EMP"};
        String[] dataTableFields = new String[]{
                "EMPNO", "ENAME", "JOB", "MGR", "HIREDATE", "SAL", "COMM", "DEPTNO"
        };
        String dataFieldName = "ENAME";

        String[] listTableNames = new String[]{"EMP"};
        String listTableField = "ENAME";

        return new ExtractDataListCmpCustomizer(filePath, fileName,
                listTableNames, listTableField, dataTableNames, dataTableFields, dataFieldName);
    }

    @Bean
    public AbstractExtractDataPageCmp baseExtractPageCmpScott() {
        String filePath = "D:\\";
        String fileName = "SCOTT_EMP_PAGE_2022060119900429.txt";
        String[] dataTableNames = new String[]{"EMP"};
        String[] dataTableFields = new String[]{
                "EMPNO", "ENAME", "JOB", "MGR", "HIREDATE", "SAL", "COMM", "DEPTNO"
        };
        return new ExtractDataPageCmpCustomizer(filePath, fileName, dataTableNames, dataTableFields, 2000);
    }

    @Bean
    public AbstractExtractDataListCmp baseExtractListCmp() {
        String filePath = "D:\\";
        String fileName = "PERSON_HOUSE_2022060119900429.txt";
        String[] dataTableNames = new String[]{"PERSON_HOUSE"};
        String[] dataTableFields = new String[]{"NAME", "CTFID", "BIRTHDAY", "MOBILE", "EMAIL"};
        String dataFieldName = "BIRTHDAY";

        String[] listTableNames = new String[]{"PERSON_HOUSE"};
        String listTableField = "BIRTHDAY";

        return new ExtractDataListCmpCustomizer(filePath, fileName,
                listTableNames, listTableField, dataTableNames, dataTableFields, dataFieldName);
    }


    @Bean
    public AbstractExtractDataPageCmp baseExtractDataCmp() {
        String filePath = "D:\\";
        String fileName = "PERSON_HOUSE_20220601.txt";
        Integer rowNum = 5000;
        String[] dataTableNames = new String[]{"PERSON_HOUSE"};
        String[] dataTableFields = new String[]{"NAME", "CTFID", "BIRTHDAY", "MOBILE", "EMAIL"};

        return (AbstractExtractDataPageCmp) ExtractDataPageCmpBuilder.builder()
                .filePath(filePath)
                .fileName(fileName)
                .dataTableNames(dataTableNames)
                .dataTableFields(dataTableFields)
                .rowNum(rowNum)
                .separator("------")
                .build(
                        (tableNames1, fields1, startIndex, rowNum1) ->
                                " SELECT *\n" +
                                        "    FROM (SELECT ROWNUM R, U.*\n" +
                                        "    FROM (SELECT " + SqlUtil.arraysToFields(fields1) + " \n " +
                                        "    FROM " + tableNames1[0] + "\n" +
                                        "    WHERE 1=1 AND CTFID LIKE '6124%'" +
                                        "    \n" +
                                        "    ) U\n" +
                                        "    WHERE ROWNUM <= " + startIndex + " + " + rowNum1 + ")\n" +
                                        "    WHERE R > " + startIndex,

                        (tableNames2, fields2) ->
                                "SELECT COUNT (*) FROM " + tableNames2[0]
                                        + " WHERE  CTFID LIKE '6124%'",
                        null);
        // return new BaseExtractDataCmp(filePath, fileName, rowNum, tableNames, fields);
    }


    @Bean
    public PersonHouseDataService personHouseDataService(PersionHouseMapper persionHouseMapper) {
        PersonHouseDataService personHouseDataService =
                new PersonHouseDataService("D:\\", "PERSON_HOUSE_20220601.txt", 2000);
        personHouseDataService.setPersionHouseMapper(persionHouseMapper);
        return personHouseDataService;
    }

    @Bean
    public ExtractDataPageSqlCmpCustomizer extractDataPageSqlCmpCustomizer() {
        String filePath = "D:\\";
        String fileName = "PERSON_HOUSE_PAGE_20220710.txt";
        Integer rowNum = 3000;
        // 统计SQL可以不用声明默认取查询SQL的数量
        String countSql = "";
        String executeSql = "SELECT NAME, CTFID, BIRTHDAY, MOBILE, EMAIL FROM PERSON_HOUSE  WHERE 1=1 AND CTFID LIKE '612429%' ";
        return new ExtractDataPageSqlCmpCustomizer(
                filePath, fileName, countSql, executeSql, rowNum);
        // FILE_PATH, FILE_NAME, COUNT_SQL, EXECUTE_SQL, ROW_NUM
        // FILE_PATH, FILE_NAME, FIELD_LIST_SQL, EXECUTE_SQL
    }

    @Bean
    public ExtractDataListSqlCmpCustomizer extractDataListSqlCmpCustomizer() {
        String filePath = "D:\\";
        String fileName = "PERSON_HOUSE_LIST_20220710.txt";
        String fieldListSql = "SELECT DISTINCT BIRTHDAY FROM PERSON_HOUSE WHERE 1=1 AND CTFID LIKE '6124291991120%'";
        String executeSql = "SELECT NAME, CTFID, BIRTHDAY, MOBILE, EMAIL " +
                "FROM PERSON_HOUSE WHERE BIRTHDAY = '#fieldValue'";
        return new ExtractDataListSqlCmpCustomizer(filePath, fileName, fieldListSql, executeSql);
    }

    @Bean
    public BatchExtractDataGroup batchExtractDataGroup(ExtractDataListSqlCmpCustomizer list,
                                                       ExtractDataPageSqlCmpCustomizer page) {

        BatchExtractDataGroup batchExtractDataGroup = new BatchExtractDataGroup();
        // 文件组抽取器配置
        HashMap<String, ExtractDataCmp> hashMap = new HashMap<>(2);
        hashMap.put("file1", list);
        hashMap.put("file2", page);
        // 文件清单属性映射配置
        HashMap<String, String> props = new HashMap<>(2);
        props.put("fileName", "FILE_NAME");
        props.put("lineCount", "LINES");
        props.put("cmpType", "TYPE");
        props.put("fileLength", "FILE_LENGTH");
        props.put("SIZE", "FILE_SIZE");
        // 组其他信息配置
        batchExtractDataGroup.setExtractDataCmpMap(hashMap);
        batchExtractDataGroup.setCreateDetailFile(true);
        batchExtractDataGroup.setBatchFilePath("D:\\" + DateUtils.getDateFormatNoLine() + "\\");
        batchExtractDataGroup.setCreateEndFile(true);
        batchExtractDataGroup.setEndFileName("end.dat");
        batchExtractDataGroup.setDetailFileProps(props);
        batchExtractDataGroup.setDetailFileName("file.txt");
        batchExtractDataGroup.setGroupName("CHANNEL_FILES_GROUP");
        batchExtractDataGroup.setGroupId("1");
        return batchExtractDataGroup;
    }


}
